package cloud.tianai.ram.policy.server.action.impl;

import cloud.tianai.ram.policy.common.model.dto.Match;
import cloud.tianai.ram.policy.common.model.dto.Statement;
import cloud.tianai.ram.policy.common.handler.ActionHandler;
import cloud.tianai.ram.policy.common.model.dto.Action;
import org.springframework.core.annotation.Order;
import org.springframework.util.AntPathMatcher;

import java.util.Collection;

/**
 * @Author: 天爱有情
 * @date 2022/3/7 14:31
 * @Description ant action 处理器
 */
@Order(20)
public class AntActionHandler implements ActionHandler {

    private AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Override
    public Match match(String currentAction, Collection<String> matchingActions) {
        for (String matchingAction : matchingActions) {
            if (antPathMatcher.match(matchingAction, currentAction)) {
                return Match.ALLOW;
            }
        }
        return Match.IMPLICIT_DENY;
    }

    @Override
    public Match handle(Action action, Statement statement) {
        return match(action.getAction(), statement.getActionList());
    }
}
